package com.haha.helloworld.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.haha.helloworld.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class BFragment extends Fragment {
    //咱们可以写一个方法接收参数
    public static BFragment newInstance(String title){
        BFragment bFragment = new BFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title",title);
        bFragment.setArguments(bundle);
        //返回已经设置好值的Fragment
        return bFragment;
    }


    private TextView tv_title;
    //用于创建视图
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //创建一个view使得我们可以在下方的onViewCreated方法中通过view，在通过id找到组件
        View view = inflater.inflate(R.layout.fragment_b,container,false);
        return view;
    }
    //创建视图后执行
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //通过之前的指定的view就可以找到我们的组件了
        tv_title= (TextView)view.findViewById(R.id.tv_title);
        //设置newInstance方法传入的title的值
        if (getArguments()!=null){
            tv_title.setText(getArguments().getString("title"));
        }

    }
}
